<?
$do_not_validate = 0; //take this out when live

include_once 'lib/facebook.php';
include_once 'includes/config.php';
include_once 'includes/dbconnect.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link href="includes/css/general_style.css" rel="stylesheet" type="text/css">

    <title><?= FACEBOOK_APP_NAME ?></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 


</head>
<body>
    <div id="fb-root"></div>
    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
    <script type="text/javascript" src="includes/js/jquery.facebook.multifriend.select.js"></script>
<link href="includes/css/jquery.facebook.multifriend.select.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
    FB.init({appId: '<?= FACEBOOK_APP_ID ?>',status: true, cookie: true,
         xfbml: true});
    	
    FB.getLoginStatus(function(response) {
        if (response.session) {
            alert("is login");
        } else {
            alert("not login");
        }
    });
</script>

<?

$loginUrl = $facebook->getLoginUrl(
        array(
            'canvas' => 1,
            'fbconnect' => 0,
            'next' => 'http://apps.facebook.com/magic-cooking/',
            'req_perms' => 'email,publish_stream,user_location,user_birthday'
        )
);

if (!$session) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
} else {
    ?>

    <script>
        function init() {
            FB.api('/me', function(response) {
                $("#username").html("<img src='https://graph.facebook.com/" + response.id + "/picture'/><div>" + response.name + "</div>");

                $("#jfmfs-container").jfmfs({ 
                    max_selected: 15, 
                    max_selected_message: "{0} of {1} selected",
                    friend_fields: "id,name,last_name",
                    sorter: function(a, b) {
                        var x = a.last_name.toLowerCase();
                        var y = b.last_name.toLowerCase();
                        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
                    }
                });
                $("#jfmfs-container").bind("jfmfs.friendload.finished", function() { 
                    window.console && console.log("finished loading!"); 
                });
                $("#jfmfs-container").bind("jfmfs.selection.changed", function(e, data) { 
                    window.console && console.log("changed", data);
                });                     
                                          
                $("#logged-out-status").hide();
                $("#show-friends").show();
                      
            });
        }              
                      
        init();  
        $("#show-friends").live("click", function() {
            var friendSelector = $("#jfmfs-container").data('jfmfs');             
            $("#selected-friends").html(friendSelector.getSelectedIds().join(', ')); 
        });       
    </script>

    <div> 
        <div id="username"></div> 
        <a href="#" id="show-friends" style="display:none;">Show Selected Friends</a> 
        <div id="selected-friends" style="height:30px"></div> 
        <div id="jfmfs-container"></div> 
    </div> 
    <?
}
?>


</body>
</html>

